﻿package fwk.sound
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import flash.utils.getDefinitionByName;
	
	import flash.events.Event;
	
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	
	
	public class CfSoundManager
	{
		public var _oSounds			:Object = new Object();
		public var _nNbSounds		:uint = 0;
		
		private var _muted:Boolean = false;
		
		/*public var _bVolumeGlobal	:Boolean = false;
		public var _nVolumeGlobal	:Number;
		public var _nVolumeMax		:Number;*/
		
		
		
		
		public function CfSoundManager():void
		{
			trace("CfSoundManager");
		}
		
		public function addSound(sId:String, loop:Object = 1):CfSound
		{
			if (_oSounds[sId] == null) {
				trace("CfSoundManager : addSound(" + sId + ")");
				var snd:CfSound = new CfSound();
				snd._manager = this;
				snd.init(sId, loop, false, _muted);
				
				
				
				_oSounds[sId] = snd;
				
				_nNbSounds++;
			}
			
			return _oSounds[sId] as CfSound;
			

		}
		public function addSoundUrl(url:String, loop:Object = 1, id:String = null):CfSound
		{
			
			var sId:String = url;
			if (id != null) sId = id;
			
			if (_oSounds[sId] == null) {
				trace("CfSoundManager : addSoundUrl(" + sId + ")");
				var snd:CfSound = new CfSound();
				snd._manager = this;
				snd.init(url, loop, true, _muted);
				
				_oSounds[sId] = snd;
				
				_nNbSounds++;
			}
			
			return _oSounds[sId] as CfSound;
		}
		public function getSound(sId:String, isExternal:Boolean = false ):CfSound
		{
			//trace("CfSoundManager : getSound(" + sId + ")");
			
			if ( _oSounds[sId] == null) {
				/*if (isExternal) addSoundUrl(sId);
				else addSound(sId);*/
				
				return null;
			}
			
			return  _oSounds[sId];
		}
		public function stopGlobal():void
		{
			//trace("CfSoundManager : stopGlobal");
			
			for (var p in _oSounds)
			{
				_oSounds[p].stop();
			}
		}
		

		public function muteGlobal(flag:Boolean, fade:Boolean = false, durationFade:Number = 1):void
		{
			//trace("CfSoundManager : stopGlobal");
			
			for (var p in _oSounds)
			{
				trace("P " +p );
				_oSounds[p].mute(flag, fade, durationFade);
			}
			
			_muted = flag;
		}
		
		public function get muted():Boolean { return _muted; }
		
		/*public function get_bVolumeGlobal():Boolean
		{
			trace("CfSoundManager : get_bVolumeGlobal");
			
			return _bVolumeGlobal;
		}
		
		public function set_bVolumeGlobal(bValue:Boolean):void
		{
			trace("CfSoundManager : set_bVolumeGlobal(" + bValue + ")");
			
			_bVolumeGlobal = bValue;
		}
		
		public function get_nVolumeGlobal():Number
		{
			trace("CfSoundManager : get_nVolumeGlobal");
			
			return _nVolumeGlobal;
		}
		
		public function set_nVolumeGlobal(nVolume:Number):void
		{
			trace("CfSoundManager : set_nVolumeGlobal(" + nVolume + ")");
			
			_nVolumeGlobal = nVolume;
			
			for (var p in _oSounds)
			{
				_oSounds[p].setVolume(nVolume);
			}
		}
		
		public function get_nVolumeMax():Number
		{
			trace("CfSoundManager : get_nVolumeMax");
			
			return _nVolumeMax;
		}
		
		public function set_nVolumeMax(nVolume:Number):void
		{
			trace("CfSoundManager : set_nVolumeMax(" + nVolume + ")");
			
			_nVolumeMax = nVolume;
			
			for (var p in _oSounds)
			{
				_oSounds[p].setVolume(nVolume);
			}
		}*/
	}
}